/**
 * Created with JetBrains PhpStorm.
 * User: lucky
 * Date: 11.05.13
 * Time: 0:09
 * To change this template use File | Settings | File Templates.
 */
function sendData(data){
   window.location.search = data;
}

function changeNumberPage(isNext){
   var args = decodeURIComponent(location.search.substr(1)).split('&'),
         currentPage,
         condition,
         number;
   if(args[0].indexOf('n=') !== -1){
      currentPage = parseInt(args[0].replace("n=", ""), 10);
      condition = isNext ? currentPage < 350 : currentPage > 1;
      if(condition){
         number = isNext ? currentPage+1 : currentPage-1;
         if(args[1].indexOf('p=') !== -1)
            sendData('n='+number+'&'+args[1]);
      }
   }
}

$(document).ready(function(){
   var img = $('.book-pdf-image');
   if(img.length > 0){
      var leftBlock = $('.book-pdf-left'),
            rightBlock = $('.book-pdf-right'),
            leftText = $('.book-pdf-text-left'),
            rightText = $('.book-pdf-text-right'),
            offset = img.offset(),
            textTop = offset.top + img.height()/2 - 15,
            offsetLeft = offset.left + img.width() - 41;
      leftBlock.css({
         'left': offset.left,
         'height': img.height()
      });
      rightBlock.css({
         'left': offsetLeft,
         'height': img.height()
      });
      leftText.css({
         'left': offset.left,
         'top' : textTop
      });
      rightText.css({
         'left': offsetLeft,
         'top' : textTop
      });
      img.hover(function(){
         leftBlock.removeClass('display-none');
         leftText.removeClass('display-none');
         rightBlock.removeClass('display-none');
         rightText.removeClass('display-none');
      }, function(){
         leftBlock.addClass('display-none');
         leftText.addClass('display-none');
         rightBlock.addClass('display-none');
         rightText.addClass('display-none');
      });
      $('.book-pdf-page').hover(function(){
         var block = $(this);
         block.removeClass('display-none');
         if(block.hasClass('book-pdf-left')){
            leftText.removeClass('display-none');
         }else{
            rightText.removeClass('display-none');
         }
      }, function(){
         var block = $(this);
         block.addClass('display-none');
         if(block.hasClass('book-pdf-left')){
            leftText.addClass('display-none');
         }else{
            rightText.addClass('display-none');
         }
      });
      $('.book-pdf-text').hover(function(){
         if($(this).hasClass('book-pdf-text-left')){
            leftBlock.removeClass('display-none');
         }else{
            rightBlock.removeClass('display-none');
         }
      }, function(){
         if($(this).hasClass('book-pdf-text-left')){
            leftBlock.addClass('display-none');
         }else{
            rightBlock.addClass('display-none');
         }
      });
      leftText.bind('click', function(){
         changeNumberPage(false);
      });
      rightText.bind('click', function(){
         changeNumberPage(true);
      });
   }
});